<?php

declare(strict_types=1);

namespace App\Controller\Admin;


use App\Model\Admin\Admin;
use App\Model\Station;
use App\Model\User;
use App\Model\White;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\HttpServer\Contract\RequestInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Trait\ResponseMsg;
use App\Model\Normal;
use Hyperf\Di\Annotation\Inject;
use App\Controller\AbstractController;

class WhiteController extends AbstractController
{
    use ResponseMsg;

    protected Redis $redis;
    protected Normal $normal;

    #[Inject]
    protected Admin $admin;


    public function __construct()
    {
        $this->redis = container()->get(Redis::class);
        $this->normal = container()->get(Normal::class);
    }

    /**白名单审核列表
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function list(RequestInterface $request):ResponseInterface{
        $page = $request->inputs(['page','limit','keyword','station_id']);
        $model = White::query()
            ->where(function ($query) use ($page) {
                if (isset($page['station_id']) && $page['station_id'] !== '') {
                    $query->where('station_id', $page['station_id']);
                }
                if (isset($page['keyword']) && $page['keyword'] !== '') {
                    $query->whereHas('user', function ($q) use ($page) {
                        $q->where('phone', 'like', '%' . $page['keyword'] . '%');
                    });
                }
            })->with('user','station');
        $data['total'] = $model->count();
        $data['list'] = $model
            ->orderBy('id', 'desc')
            ->offset(($page['page'] - 1) * $page['limit'])
            ->limit($page['limit'])
            ->get()->toArray();
        return $this->success($data);
    }

    /**白名单添加
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function create(RequestInterface $request):ResponseInterface{
        $station_id = $request->input('station_id');
        $phone = $request->input('phone');
        $user  = User::where('phone',$phone)->first();
        if(!$user){
            return $this->error(1001,'用户不存在');
        }
        $check = White::where('user_id',$user->id)
            ->where('station_id',$station_id)->first();
        if($check){
            return $this->error(1001,'白名单信息已存在');
        }
        try{
             $model = new White();
             $model->user_id = $user->id;
             $model->station_id = $station_id;
             $model->save();
             $keys = 'WHITE_DEVICE_'.$station_id;
             $this->redis->sAdd($keys,$user->id);
             return $this->success();
        }catch (\Exception $e){
            Normal::logger($e->getMessage() . $e->getTraceAsString(), 'home');
            return $this->error(1001,$e->getMessage());
        }
    }


    /**为设备移除白名单
     * @param RequestInterface $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function del(RequestInterface $request):ResponseInterface{
        $id = $request->input('id');
        $model = White::where('id',$id)->first();
        $keys = 'WHITE_DEVICE_'.$model->station_id;
        redis()->sRem($keys,$model->user_id);
        $model->delete();
        return $this->success();
    }
}
